package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Attachment;
import com.ruoyi.system.service.AttachmentService;
import org.springframework.data.domain.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 附件管理表(Attachment)表控制层
 */
@Slf4j
@RestController
@RequestMapping("/attachment")
public class AttachmentController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private AttachmentService attachmentService;

//    /**
//     * 分页查询
//     *
//     * @param attachment 筛选条件
//     * @return 查询结果
//     */
//    @GetMapping("/list")
//    public TableDataInfo list(Attachment attachment) {
//        startPage();
//        List<Attachment> attachments = attachmentService.queryList(attachment);
//        return getDataTable(attachments);
//    }

    /**
     * 根据课程id和附件类型查询
     * @param courseId
     * @param attachmentType
     * @return
     */
    @GetMapping
    public TableDataInfo getAttachmentByCourseIdAndType(@RequestParam("courseId") String courseId, @RequestParam("attachmentType") String attachmentType){
        startPage();
        List<Attachment> list = attachmentService.getAttachmentByCourseIdAndType(courseId, attachmentType);
        return getDataTable(list);
    }

    /**
     * 分页条件查询
     *
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo list(Attachment attachment) {
        startPage();
        List<Attachment> list = attachmentService.list(attachment);
        return getDataTable(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping(value = "{attachmentId}")
    public AjaxResult queryById(@PathVariable("attachmentId") Long id) {
        return success(attachmentService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param attachment 实体
     * @return 新增结果
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Attachment attachment){
        attachment.setCreateBy(getUsername());
        attachment.setCreateTime(DateUtils.getNowDate());
        return success(attachmentService.insert(attachment));
    }

    /**
     * 编辑数据
     *
     * @param attachment 实体
     * @return 编辑结果
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Attachment attachment) {
        attachment.setUpdateBy(getUsername());
        attachment.setUpdateTime(DateUtils.getNowDate());
        return success(attachmentService.update(attachment));
    }

    /**
     * 删除数据
     *
     * @param attachmentId 主键
     * @return 删除是否成功
     */
    @DeleteMapping(value = "{attachmentId}")
    public AjaxResult deleteById(@PathVariable Long attachmentId) {
        return success(attachmentService.deleteById(attachmentId));
    }

}
